package com.webapp.org.controller;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.webapp.org.bean.DepartmentBean;
import com.webapp.org.service.DepartmentService;

/**
 * Servlet implementation class DepartmentServlet
 */
@WebServlet("/DepartmentServlet.do")
public class DepartmentServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		DepartmentBean departmentBean = null;
		RequestDispatcher rd = null;
		boolean result = false;
		int departmentSrno = request.getIntHeader("departmentSrno");
		String departmentName = request.getParameter("depatmentName");
		String departmentRemark = request.getParameter("depatmentRemark");
		String submit = request.getParameter("submit");
		
		
		//departname for insertion or srno for update and delete
				if (departmentName.length() > 0 )
				{
					try
					{
						departmentBean = new DepartmentBean();
									
					    if(submit.equalsIgnoreCase("insert"))
						{
					    	departmentBean.setDepartmentName(new String(departmentName.getBytes("8859_1"), "UTF-8").toString());
							departmentBean.setRemark(new String(departmentRemark.getBytes("8859_1"), "UTF-8").toString());
					    	result = new DepartmentService().insertDepartment(departmentBean);
						    System.out.println(departmentBean.toString()+" --insert--- "+result);
						}
					    else if(submit.equalsIgnoreCase("update"))
					    {
					    	departmentBean.setDeptId(departmentSrno);
					    	departmentBean.setDepartmentName(new String(departmentName.getBytes("8859_1"), "UTF-8").toString());
							departmentBean.setRemark(new String(departmentRemark.getBytes("8859_1"), "UTF-8").toString());
					    	result = new DepartmentService().updateDepartment(departmentBean);
						    System.out.println(departmentBean.toString()+" --update--- "+result);
					    }
					    else if((submit.equalsIgnoreCase("delete")))
					    {
					    	departmentBean.setDeptId(departmentSrno);
					    	result = new DepartmentService().deleteDepartment(departmentBean.getDeptId());
						    System.out.println(departmentBean.toString()+" --delete--- "+result);
					    }
					}
					catch (java.io.UnsupportedEncodingException e) 
					{
						System.err.println(e);
					}
				} 
				if(result)
				{
					rd = request.getRequestDispatcher("jsp_pages/HomePage.jsp?status=success");
							}
				else	// error comes to directly send to jsp page
				{
					rd = request.getRequestDispatcher("error.jsp");
				
				}
		         rd.forward(request, response); // forward to RequestDispatcher
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		doGet(request, response);
	}

}
